package com.demo.controller;

import com.demo.common.Result;
import com.demo.pojo.dto.StudentListDTO;
import com.demo.pojo.entity.Student;
import com.demo.service.StudentService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Author：潘建冰
 * Package：com.demo.controller
 * Project：demo
 * name：StudentController
 * Date：2025/9/11  17:20
 */

@RestController
@RequestMapping("/student")
public class StudentController {


    private final StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }


    /**
     * 根据id查询学生数据
     * @param id 学生id
     * @return Result
     */
    @GetMapping("/select")
    public Result<Student> select(@RequestParam Integer id){
        return Result.success(studentService.selectById(id));
    }


    /**
     * 保存学生数据
     * @param student 学生数据
     * @return  Result
     */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody Student student){
        studentService.saveStudent(student);
        return Result.success();
    }


    /**
     * 修改学生数据
     * @param student 学生数据
     * @return  Result
     */
    @PutMapping("/update")
    public Result<Void> update(@RequestBody Student student){
        studentService.updateStudent(student);
        return Result.success();
    }

    /**
     * 删除学生数据
     * @param id 学生id
     * @return  Result
     */
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam Integer id){
        studentService.removeById(id);
        return Result.success();
    }

    /**
     * 分页查询学生数据
     * @param pageNum 页码
     * @param pageSize 每页数据量
     * @param stNo 学生学号
     * @param stName 学生姓名
     * @param stSex 学生性别
     * @param stPhone 学生手机号
     * @param stEmail 学生邮箱
     * @param stQq 学生QQ
     * @param stClassId 班级ID
     * @return  Result
     */
    @GetMapping("/listPage")
    public Result<Map<String, Object>> listPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(required = false) Long stNo,
                                @RequestParam(required = false) String stName,
                                @RequestParam(required = false) Integer stSex,
                                @RequestParam(required = false) String stPhone,
                                @RequestParam(required = false) String stEmail,
                                @RequestParam(required = false) String stQq,
                                @RequestParam(required = false) Long stClassId){
        //封装DTO对象进行传递
        StudentListDTO studentListDTO = new StudentListDTO();
        studentListDTO.setStNo(stNo);
        studentListDTO.setStName(stName);
        studentListDTO.setStSex(stSex);
        studentListDTO.setStPhone(stPhone);
        studentListDTO.setStEmail(stEmail);
        studentListDTO.setStQq(stQq);
        studentListDTO.setStClassId(stClassId);
        studentListDTO.setPageNum(pageNum);
        studentListDTO.setPageSize(pageSize);
        return Result.success(studentService.listPage(studentListDTO));
    }

}


